﻿using System;
using System.Collections.Generic;
using System.Data;

namespace ApplicationTemplate
{
    class CreateDacUpdateAPI
    {
        public static String CreateUpdate(String realTableName, String modelClassName, DataSet FieldsDs, String DAMInstanceName)
        {
            String tableName = ApplcationTemplate.GetTableName(realTableName);
            return String.Format(
@"public int Update{0}({1} model, DBManager dbManager)
        {{
            return {2}.Update{0}(model, dbManager);
        }}",
            tableName,
            modelClassName,
            DAMInstanceName);
        }

        public static String CreateUpdateNoDbManager(String realTableName, String modelClassName, DataSet FieldsDs, String DAMInstanceName)
        {
            String tableName = ApplcationTemplate.GetTableName(realTableName);
            return String.Format(
@"public int Update{0}({1} model)
        {{
            using (DBManager dbManager = {2}.BeginDAMTransaction())
            {{
                try
                {{
                    int result = {2}.Update{0}(model, dbManager);
                    dbManager.Commit();
                    return result;
                }}
                catch (Exception ex)
                {{
                    dbManager.Rollback();
                    SicsExceptionPolicy.HandleException(ex, SicsExceptionPolicy.ExceptionPolicyName.ExceptionPolicy);
                    return -1;
                }}
            }}
        }}",
            tableName,
            modelClassName,
            DAMInstanceName);
        }
    }
}
